package com.social.servlet;




import com.social.dao.LogDao;
import com.social.model.Comment;
import com.social.model.User;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@WebServlet(name = "Add_Comment", value = "/Add_Comment")
public class Add_Comment extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.sendRedirect("home.jsp");
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html; charset=UTF-8");
//       fetch传过来的值


int logId = Integer.parseInt(request.getParameter("logId"));
        String content = request.getParameter("content");
//        int logId = (int) session.getAttribute("logId");

//        这里的user是通过登录时设置的session传过来的，在loginservlet那里
        User currentUser = (User) request.getSession().getAttribute("user");

//        userId的时候可以存session里面

//        这两个也需要你去数据库调用

//        方法已经写好了，叫做getnickName_avatar
        //用自己的表验证了一下，成功，还没有用别人的表进行验证


        String nickname = currentUser.getNickname();
        String avatar = currentUser.getAvatar();


        Comment c = new Comment(logId, nickname, avatar, content);

        LogDao l = new LogDao();

        l.addComment(c);





//        String[] result = new String[2];
//        result = l.getNickname_Avatar("li");
//        System.out.println(result[0]+result[1]);

    }
}
